﻿using System;
using System.Windows.Forms;

namespace 物流管理系统
{
    public partial class Carsinfo : Form
    {
        DBOperation dboperation = new DBOperation();
        string sqlstr;
        public int formtype;
        ReMatch re = new ReMatch();
        public Carsinfo()
        {
            InitializeComponent();
        }
        private void confirm_Click(object sender, EventArgs e)
        {
            foreach (Control c in Controls)
            {
                if (c is TextBox txt)
                {
                    if (txt.Text==""&&(!c.Name.Equals("remarkstext")))
                    {
                        MessageBox.Show("有重要项目为空，请检查！！！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                if (c is ComboBox cmb)
                {
                    if (cmb.Text=="")
                    {
                        MessageBox.Show("有重要项目为空，请检查！！！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }
            if (!re.caridmatch(idtext.Text))
            {
                MessageBox.Show("编号输入非法！！！\n格式为英文和数字组成的5-15位字符串。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!re.ldmatch(modeltext.Text))
            {
                MessageBox.Show("型号输入非法！！！\n格式为中英文组成的字符串。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!(re.intmatch(loadcapacitytext.Text)||re.decmatch(loadcapacitytext.Text)))
            {
                MessageBox.Show("载重量输入非法！！！\n格式为整数或1，2位小数数字。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!re.adressmatch(startpointtext.Text))
            {
                MessageBox.Show("发车站输入非法！！！\n格式为中英文和数字组成的2位以上地址。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (formtype)
            {
                case 0://添加
                    {
                        if (dboperation.exists("select "+idlabel.Text+" from "+Text+"表 where "+idlabel.Text+"='"+idtext.Text+"'", idtext.Text))
                        {
                            MessageBox.Show("此车辆已经存在！！！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        else
                        {
                            sqlstr="insert into 车辆信息表 VALUES('"+idtext.Text+"','"+modeltext.Text+"','"+driveridcombobox.Text+"','"+loadcapacitytext.Text+"','"+startpointtext.Text+"','"+getcheckbox(loadcheckbox)+"','"+getcheckbox(availablecheckbox)+"');";
                        }
                        break;
                    }
                case 1://修改
                    {
                        sqlstr="replace into 车辆信息表 VALUES('"+idtext.Text+"','"+modeltext.Text+"','"+driveridcombobox.Text+"','"+loadcapacitytext.Text+"','"+startpointtext.Text+"','"+getcheckbox(loadcheckbox)+"','"+getcheckbox(availablecheckbox)+"');";
                        break;
                    }
                case 2://查看
                    {
                        Close();
                        break;
                    }
            }
            dboperation.nonquerysql(sqlstr);
            Close();
        }
        private string getcheckbox(CheckBox checkBox)
        {
            if (checkBox.Checked)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }
        private void Carsinfo_Load(object sender, EventArgs e)
        {
            switch (formtype)
            {
                case 0://添加
                    {
                        break;
                    }
                case 1://修改
                    {
                        idtext.Enabled=false;
                        break;
                    }
                case 2://查看
                    {
                        foreach (Control c in Controls)
                        {
                            if (!(c is Button))
                            {
                                c.Enabled=false;
                            }
                        }
                        break;
                    }
            }
        }

    }
}
